<?php
namespace app\index\controller;
use app\index\controller\Base;
use app\index\model\Set;
use app\index\model\Good;
use app\index\model\Coupon;
use app\index\model\Instance;
use app\index\model\User;
use app\index\model\Log;

use think\Request;
class Tb extends Base{
	private $shop_id='';
	private $open_id='';
    public function __construct(){
    	$this->shop_id=Request::instance()->param('shop_id');
    	$this->open_id=Request::instance()->param('open_id');
    }
    public function index(){
    	$open_id=Request::instance()->param('open_id');
    	$set=Set::get(['shop_id'=>$this->shop_id]);
    	$coupon=Coupon::get(['shop_id'=>$this->shop_id]);
    	$good=Good::all(['shop_id'=>$this->shop_id]);
    	$set['coupon']=$coupon;
    	$set['good']=$good;
    	$followerNum=$set->followerNum;
    	$user=User::get(['open_id'=>$open_id]);
    	$follower=$user->follower;
    	$willShare=$followerNum-$follower;
    	$set['follower']=$follower;
    	$set['willShare']=$willShare;
    	$end=strtotime($set->end);//结束日期转换为时间戳
    	if($end<time()){
    		$set['timeStamp']=0;
    	}else{
    		$set['timeStamp']=$end-time();
    	}
    	$this->success($set);
    }
    public function getCoupon(){
    	$open_id=Request::instance()->param('open_id');
    	$user=User::get(['open_id'=>$open_id,'shop_id'=>$this->shop_id]);
    	$set=Set::get(['shop_id'=>$this->shop_id]);
    	$followerNum=$set->followerNum;
    	if($isFollow!=0){
    		$this->success('请先关注店铺');
    	}
    	if($user->follower>=$followerNum){
    		$this->success('领取成功');
    	}else{
    		$this->success('尚未达到领取条件');
    	}
    }
    public function writeLog(){
    	$data=Request::instance()->param();
    	$main_nickName=Request::instance()->param('main_nickName');
    	$log=new Log($data);
    	$log->allowField(true)->save();
    	$user=User::get(['nickName'=>$main_nickName,'shop_id'=>$this->shop_id]);
    	$user->follower+=1;
    	$user->allowField(true)->save();
    	$this->success($data);

    }
    public function addUser(){
    	$data=Request::instance()->param();
    	$user=new User($data);
    	$user->allowField(true)->save();
    	$this->success($data);
    }

}